<?php
/**
 * Created by PhpStorm.
 * User: Sergey
 * Date: 22.03.14
 * Time: 16:15
 */

include_once BASE_DQ_PLUGIN_PATH . "classes/Config.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/ConnectionFactory.php";
require_once BASE_DQ_PLUGIN_PATH . "classes/User.php";

class UserDao extends ConnectionFactory
{
    const ROLE_TUTOR = "author";
    const ROLE_STUDENT = "contributor";

    private function fetch($row)
    {
        try {
            $user = new User();
            $user->id = $row->ID;
            $user->displayName = $row->display_name;
            return $user;
        } catch (Exception $ex) {
            return null;
        }
    }

    private function getGroups($userId, $role)
    {
        $table = "";
        switch ($role) {
            case self::ROLE_TUTOR:
                $table = Config::get()->getRelTutorToGroupsTableName();
                break;
            case self::ROLE_STUDENT:
                $table = Config::get()->getRelStudentToGroupTableName();
                break;
        }

        $query = "SELECT * FROM $table WHERE user_id=$userId";

        global $wpdb;
        $result = $wpdb->get_results($query);

        $groups = array();
        foreach ($result as $row) {
            $groups[] = $row->group_id;
        }
        return $groups;
    }

    public function getAll($role)
    {
        $result = get_users(["role" => $role]);
        $tutors = array();
        foreach ($result as $data) {
            $tutor = $this->fetch($data);
            $tutor->groups = $this->getGroups($tutor->id, $role);
            $tutors[] = $tutor;
        }
        return $tutors;
    }

    public function getAllStudentsBy($groupId)
    {
        $table = Config::get()->getRelStudentToGroupTableName();
        $query = "SELECT * FROM $table WHERE group_id=$groupId";

        global $wpdb;
        $result = $wpdb->get_results($query);

        $students = array();
        foreach ($result as $row) {
            $students[] = $this->getById(
                $row->user_id,
                self::ROLE_STUDENT
            );
        }
        return $students;
    }

    public function getById($id, $role)
    {
        $data = get_userdata($id);
        $user = $this->fetch($data);
        $user->groups = $this->getGroups($user->id, $role);
        return $user;
    }

    public function edit(User $user, $role)
    {
        $table = "";
        switch ($role) {
            case self::ROLE_TUTOR:
                $table = Config::get()->getRelTutorToGroupsTableName();
                break;
            case self::ROLE_STUDENT:
                $table = Config::get()->getRelStudentToGroupTableName();
                break;
        }

        global $wpdb;
        $wpdb->delete(
            $table,
            array("user_id" => $user->id),
            array("%d")
        );

        global $wpdb;
        foreach ($user->groups as $groupId) {
            $wpdb->insert(
                $table,
                array(
                    "user_id" => $user->id,
                    "group_id" => $groupId
                ),
                array("%d", "%d")
            );
        }
    }
} 